database = {}
def updateDatabase(database, client, timeStamp, factor, timeLen):
    if client not in database:
        database[client] = {}
    if timeStamp not in database[client]:
        database[client][timeStamp] = {}
    if factor in database[client][timeStamp]:
        return 
    database[client][timeStamp][factor] = timeLen

n = int(input())
for _ in range(n):
    info = input().split(',')
    timeStamp = info[0]
    client = info[1]
    factor = info[2]
    timeLen = int(info[3])
    if timeLen < 0 or timeLen > 100:
        timeLen = 0
    updateDatabase(database, client, timeStamp, factor, timeLen)


m = int(input())
f = {}
for _ in range(m):
    info = input().split(',')
    factor = info[0]
    value = int(info[1])
    f[factor] = value
    
fees = {}
clients = list(database.keys())
clients.sort()
for c in clients:
    fees[c] = 0
    for stamp in database[c]:
        for factor in database[c][stamp]:
            if factor in f:
                fees[c] += f[factor] * database[c][stamp][factor]
            
for c in fees:
    print(c, fees[c])



